package com.yyou.chat_gpt.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yyou.chat_gpt.entity.ChatHistoryInfo;
import com.yyou.chat_gpt.entity.UserInfo;
import com.yyou.chat_gpt.service.ChatHistoryService;
import com.yyou.chat_gpt.service.UserService;
import com.yyou.chat_gpt.util.MiscU;
import com.yyou.chat_gpt.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ChatHistoryController
 * @Author ZengRJ
 * @Date 2023/8/9 19:29
 * @Version 1.0
 */
@RestController
@RequestMapping("/core/history")
@Slf4j
public class ChatHistoryController {

    @Resource
    private ChatHistoryService chatHistoryService;

    @Resource
    private UserService userService;

    @PostMapping("/addHistory")
    public Result addHistory(@RequestBody ChatHistoryInfo chatHistoryReq) {
        log.info("ChatHistoryReq: " + chatHistoryReq.toString());
        //params: {type=msg, msg=你好！有什么我可以帮助你的吗？, my=false, date=2023年08月09日 19:42}
        ChatHistoryInfo chatHistoryInfo = new ChatHistoryInfo();
        BeanUtils.copyProperties(chatHistoryReq, chatHistoryInfo);

        Object loginId = StpUtil.getLoginId();
        UserInfo userInfo = userService.getUserInfo(loginId);
        chatHistoryInfo.setUserId(userInfo.getId());

        try {
            if (chatHistoryService.insertChatHistory(chatHistoryInfo) > 0) {
                return Result.success();
            } else {
                return Result.failure("插入chat消息失败");
            }
        } catch (Exception e) {
            log.error("插入chat消息失败,{}", e);
            return Result.failure("插入chat消息失败");
        }
    }

    @PostMapping("/getHistory")
    public Result getHistory() {
        Object loginId = StpUtil.getLoginId();
        UserInfo userInfo = userService.getUserInfo(loginId);
        log.info("userInfo: {}", userInfo);
        List<ChatHistoryInfo> chatHistoryList = chatHistoryService.getChatHistoryList(userInfo);
        log.info("chatHistoryList: {}", chatHistoryList);
        return Result.success(MiscU.Instance.toMap("chatHistory", chatHistoryList));
    }

    @PostMapping("/clearHistory")
    public Result clearHistory() {
        try {
            Object loginId = StpUtil.getLoginId();
            UserInfo userInfo = userService.getUserInfo(loginId);
            log.info("userInfo: {}", userInfo);
            chatHistoryService.clearChatHistory(userInfo);
            log.info("清除聊天历史成功");
            return Result.success("清除聊天历史成功");
        } catch (Exception e) {
            log.error("清除聊天历史失败, {}", e);
            return Result.failure("清除聊天历史失败");
        }
    }
}
